package com.interview.customer.service;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.interview.contacts.entity.Contacts;
import com.interview.contacts.entity.ContactsListForm;
import com.interview.contacts.query.ContactsConditionQuery;
import com.interview.contacts.respository.ContactsRespository;
import com.interview.core.page.Page;
import com.interview.customer.entity.Customer;
import com.interview.customer.query.CustomerConditionQuery;
import com.interview.customer.respository.CustomerRespository;

@Service
public class CustomerService {
	@Resource
	private CustomerRespository customerRespository;

	public List<Customer> findAllCustomer() {
		return customerRespository.findAll();
	}

	public Customer findOneCustomer(String id) {
		return customerRespository.findOne(id);
	}

	public Page findByCustomersCustomerConditionQuery(
			CustomerConditionQuery conditionQuery) {
		return customerRespository
				.findByCustomersCustomerConditionQuery(conditionQuery);
	}

	public Customer saveCustomer(Customer customer) {
		customerRespository.save(customer);
		return customer;
	}

	public Customer updateCustomer(Customer customer) {
		customerRespository.update(customer);
		return customer;
	}

	public void deleteCustomerById(String id) {
		customerRespository.delete(id);
	}
//-----------------------------------------------------------------------------------
	@Resource
	private ContactsRespository contactsRespository;

	public List<Contacts> findAllContacts() {
		return contactsRespository.findAllContacts();
	}

	public Contacts findOneContact(String id) {
		return contactsRespository.findOne(id);
	}

	public Page findContactsByContactsConditionQuery(
			ContactsConditionQuery conditionQuery) {
		return contactsRespository
				.findContactsByContactsConditionQuery(conditionQuery);
	}

	public Contacts saveContacts(Contacts contacts) {
		contactsRespository.save(contacts);
		return contacts;
	}

	public void deleteContacts(String id) {
		contactsRespository.delete(id);
	}

	public Contacts updateContacts(Contacts contacts) {
		contactsRespository.update(contacts);
		return contacts;
	}

	// ----------------------------------------------------------------------------------------------

	public void deleteCustomer(String id) {

		
		customerRespository.delete(id);
	}

	public Customer updateCustomerAndContactsListForm(Customer customer,
			ContactsListForm contactsListForm) {

		
	
			List<Contacts> contactsList = contactsListForm.getContacts();
			for (Iterator iterator = contactsList.iterator(); iterator
					.hasNext();) {

				Contacts contacts = (Contacts) iterator.next();
				String address = contacts.getAddress();
				String duty = contacts.getDuty();
				String mail = contacts.getMail();
				String name = contacts.getName();
				String tel = contacts.getTel();

				if (address == null & duty == null & mail == null
						& name == null & tel == null) {
					iterator.remove();

				}

			}
			
			customer.setContacts(contactsList);
		

		customerRespository.update(customer);
		return customer;
	}

	public Customer saveCustomerAndContactsListForm(Customer customer,
			ContactsListForm contactsListForm) {

		
			List<Contacts> contactsList = contactsListForm.getContacts();
			for (Iterator iterator = contactsList.iterator(); iterator
					.hasNext();) {

				Contacts contacts = (Contacts) iterator.next();
				String address = contacts.getAddress();
				String duty = contacts.getDuty();
				String mail = contacts.getMail();
				String name = contacts.getName();
				String tel = contacts.getTel();

				if (address == null & duty == null & mail == null
						& name == null & tel == null) {
					iterator.remove();

				}

			}
			
			customer.setContacts(contactsList);
	
			
		
		
		customerRespository.save(customer);
		return customer;
	}
}
